package com.lhl.fw.db.biz.domain.entity;

import com.lhl.fw.db.entity.BaseEntity;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

@Entity
@Getter
@Setter
@NoArgsConstructor
public class Menu extends BaseEntity<String> {

	private String code;
	private String name;
	@ManyToOne(cascade= {CascadeType.PERSIST,CascadeType.MERGE},fetch=FetchType.LAZY)
	@JoinColumn(name="p_id")
	private Menu menu;
	@OneToMany(mappedBy="menu",cascade=CascadeType.ALL)
	private Set<Menu> menus = new HashSet<Menu>();
	
	public void addMenu(Menu menu) {
		this.menus.add(menu);
		menu.setMenu(this);
	}
	
	public void removeMenu(Menu menu) {
		if(this.menus.contains(menu)) {
			menu.setMenu(null);
			this.menus.remove(menu);
		}
	}
}
